var sys = require('sys');

exports.create = function() {
  return new Queue();
}

function Queue() {
  this.max = 10;
  this.playerList = [];
  this.countdown;
  this.inCountdown = false;
  this.countdownMilliseconds = 10000;
  this.maxPlayers = 2;
  this.countdownFinished;
  
  sys.log("Queue:construct");
  
  this.size = function() {
    return this.playerList.length;
  }
  
  this.get = function(id) {
    for(var i = 0; i < this.playerList.length; i++)
    {
      if(this.playerList[i].id == id)
        return this.playerList[i];
    }

    return null;
  }
  
  this.contains = function(id) {
    return (this.get(id));
  }
  
  this.add = function(newPlayer) {
    sys.log("QueuePool:add");
    
    this.playerList.push(newPlayer);
  }
  
  this.full = function() {
    return (this.size() >= this.maxPlayers);
  }
  
  this.startCountdown = function() {
    this.inCountdown = true;
    this.countdown = this.countdownMilliseconds;
    
    var self = this;
    setTimeout(function() {
      self.countdown -= 100;
      
      if(self.countdown == 0) {
        self.inCountdown = false;
        
        // Emit finished event to the queuepool
        if(self.countdownFinished)
          self.countdownFinished.call();
      }
      else
        setTimeout(arguments.callee, 100);
    }, 100);
  }
}